cmake_minimum_required(VERSION 3.11)

set(PLUGIN_NAME "SymPy" CACHE STRING "插件名")
set(PLUGIN_VERSION "1.0.0" CACHE STRING "插件版本")
option(BEX_BUILD_PLUGIN "编译为插件而不是工具箱" ON)
project(${PLUGIN_NAME} VERSION ${PLUGIN_VERSION})

# 禁止在顶层目录编译
set(CMAKE_DISABLE_SOURCE_CHANGES ON)
set(CMAKE_DISABLE_IN_SOURCE_BUILD ON)

# 设置 C99 编译标准
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED True)

# 设置 C++17 编译标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

# 设置Release版本的编译参数
set(CMAKE_C_FLAGS_RELEASE "-O3 -DNDEBUG")
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG")

# 设置 CMAKE_BUILD_TYPE
if ("${CMAKE_BUILD_TYPE}" STREQUAL "")
    message(STATUS "CMAKE_BUILD_TYPE is unset, defaulting to Debug")
    set(CMAKE_BUILD_TYPE "Debug")
endif()


# 复制脚本测试文件和配置文件
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    file(GLOB_RECURSE TEST_SCRIPTS ${PROJECT_SOURCE_DIR}/tests/*)
    file(COPY ${TEST_SCRIPTS} DESTINATION ${PROJECT_BINARY_DIR}/${PLUGIN_NAME}/scripts)
    file(GLOB_RECURSE TEST_SCRIPTS ${PROJECT_SOURCE_DIR}/examples/*)
    file(COPY ${TEST_SCRIPTS} DESTINATION ${PROJECT_BINARY_DIR}/${PLUGIN_NAME}/scripts)
    add_definitions(-DBALTAM_DEBUG -DBEX_BUILD_PLUGIN -DDEBUG)
endif()
file(COPY ${PROJECT_SOURCE_DIR}/config.json DESTINATION ${PROJECT_BINARY_DIR}/${PLUGIN_NAME})
file(COPY ${PROJECT_SOURCE_DIR}/examples DESTINATION ${PROJECT_BINARY_DIR}/${PLUGIN_NAME})
file(COPY ${PROJECT_SOURCE_DIR}/scripts DESTINATION ${PROJECT_BINARY_DIR}/${PLUGIN_NAME})

# 将所有 src/*.cpp \文件列出来
file(GLOB_RECURSE CXX_SOURCE ${PROJECT_SOURCE_DIR}/src/*.cpp)

# 这里 SOURCE 表示所有用于编译成内置函数库的 cpp 文件
set(SOURCE ${CXX_SOURCE})

# 设置 CMake 选项
option(BUILD_SHARED_LIBS "Enable shared library. Default: ON" ON)
set(PACKAGE_NAME_SUFFIX "" CACHE STRING "Change the suffix of the package name. Default is empty")
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
    add_compile_options(/utf-8 /permissive- /Zc:__cplusplus)
endif()

# baltam SDK
if ("${BALTAM_SDK_DIR}" STREQUAL "")
    set(BALTAM_SDK_DIR "deps/core")
endif()

# 导出模块
add_library(${PROJECT_NAME} SHARED ${SOURCE})
set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME "main")
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "")
if(BEX_BUILD_PLUGIN) # 编译为插件而不是工具箱
    target_compile_definitions(${PROJECT_NAME} PRIVATE BEX_BUILD_PLUGIN)
endif()

include_directories(
    "${PROJECT_SOURCE_DIR}/src"
    "${PROJECT_SOURCE_DIR}/deps/python/include/python"
    "${PROJECT_SOURCE_DIR}/deps/bexpython/include"
    "${PROJECT_SOURCE_DIR}/deps/core/include")

target_link_directories(${PROJECT_NAME} PRIVATE 
${CMAKE_CURRENT_LIST_DIR}/deps/core/lib
${CMAKE_CURRENT_LIST_DIR}/deps/bexpython/lib)


if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
target_link_libraries(${PROJECT_NAME} PRIVATE libbex libbexPython)
else()
target_link_libraries(${PROJECT_NAME} PRIVATE bex bexPython)
endif()

# 设置输出目录
set_target_properties(${PROJECT_NAME} PROPERTIES
    LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/${PLUGIN_NAME}
    RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/${PLUGIN_NAME}
)

# 去除多余符号
if (CMAKE_BUILD_TYPE STREQUAL "Release")
    set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS_RELEASE -s)
    if (WIN32)
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--exclude-all-symbols")
    else()
        set_target_properties(${PROJECT_NAME} PROPERTIES
            CXX_VISIBILITY_PRESET hidden
            VISIBILITY_INLINES_HIDDEN ON)
    endif()
endif()

# 打包
set(CPACK_GENERATOR "ZIP")
set(CPACK_STRIP_FILES ON)